
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.List;

public class Transfer extends JPanel implements ActionListener {
    private Icon transferImage = Util.getIcon("/image/pane.jpg");//转账按钮图
    private Icon backImage = Util.getIcon("/image/pane.jpg");//返回按钮图
    private ATM frame;//顶级容器实例
    private List<User> usersList;//用户信息集合实例
    private JButton backBtn;//返回按钮
    private JLabel jLabelBack;//返回文本
    private JLabel transferUserJLabel;//转账用户提示插件
    private JTextField iptTransferUser;//转账用户输入框
    private JLabel transferMoneyJLabel;//转账金额提示插件
    private JTextField iptTransferMoney;//转账金额输入框
    private JButton transferBtn;//转账按钮
    private JLabel transferJLabel;
    private int transferUserIndex = -1;//要转账人的下标
    private double transferMoney;
    private Font font = new Font("宋体", Font.PLAIN, 20);
    public Transfer(ATM frame) {
        this.frame = frame;
        //设置空布局和透明度
        this.setLayout(null);
        this.setOpaque(false);
        //转账用户提示插件
        transferUserJLabel = new JLabel("输入收款人账户：");
        transferUserJLabel.setBounds(240, 190, 150, 50);
        transferUserJLabel.setFont(font);
        this.add(transferUserJLabel);
        //转账用户输入框
        iptTransferUser = new JTextField();
        iptTransferUser.setBounds(380, 200, 300, 30);
        iptTransferUser.setFont(font);
        this.add(iptTransferUser);
        //转账金额提示插件
        transferMoneyJLabel = new JLabel("输入转账金额：");
        transferMoneyJLabel.setBounds(240, 240, 150, 50);
        transferMoneyJLabel.setFont(font);
        this.add(transferMoneyJLabel);
        //转账金额输入框
        iptTransferMoney = new JTextField();
        iptTransferMoney.setBounds(380, 250, 300, 30);
        iptTransferMoney.setFont(font);
        this.add(iptTransferMoney);
        //转账按钮
        transferJLabel = new JLabel("转  账");
        transferJLabel.setBounds(900, 220, 140, 50);
        transferJLabel.setFont(new Font("宋体", Font.BOLD, 25));
        transferJLabel.setForeground(Color.white);
        this.add(transferJLabel);
        transferBtn = new JButton(transferImage);
        transferBtn.setBounds(870, 220, 140, 50);
        transferBtn.setOpaque(false);
        this.add(transferBtn);
        transferBtn.addActionListener(this);
        //返回按钮
        jLabelBack = new JLabel("返  回");
        jLabelBack.setBounds(900, 300, 100, 50);
        jLabelBack.setForeground(Color.white);
        this.add(jLabelBack);
        jLabelBack.setFont(new Font("宋体", Font.BOLD, 25));
        backBtn = new JButton(backImage);
        backBtn.setBounds(870, 300, 140, 50);
        backBtn.setOpaque(false);
        this.add(backBtn);
        backBtn.addActionListener(this);//建立联系
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == backBtn) {
            iptTransferUser.setText("");
            iptTransferMoney.setText("");
            frame.switchPanel("menu");
        } else if (e.getSource() == transferBtn) {
            double loginUserMoney = frame.getLoginUser().getBalance();//登陆用户的金额
            try {
                transferMoney = Double.parseDouble(iptTransferMoney.getText()); //输入转账的金额
            } catch (NumberFormatException exception) {
                JOptionPane.showMessageDialog(this, "输入金额为空，或格式错误");
                return;
            }
            double transferUserMoney = 0;//要转账人的金额
            boolean find = false;//找没有找到该用户
            if (frame.getLoginUser().getAccount().equals(iptTransferUser.getText())) {
                JOptionPane.showMessageDialog(this, "不能转给自己，请重新输入");
                return;
            }
            for (int i = 0; i < frame.getUsersList().size(); i++) {
                if (frame.getUsersList().get(i).getAccount().equals(iptTransferUser.getText())) {
                    transferUserMoney = frame.getUsersList().get(i).getBalance();
                    find = true;
                    transferUserIndex = i;
                }
            }
            if (find) {
                if (loginUserMoney >= transferMoney) {//钱够，可以转
                    if (transferMoney % 100 != 0) {
                        JOptionPane.showMessageDialog(this, "请输入100的倍数");
                        return;
                    }
                    frame.getUsersList().get(transferUserIndex).setBalance(transferUserMoney + transferMoney);
                    frame.getLoginUser().setBalance(loginUserMoney - transferMoney);
                    JOptionPane.showMessageDialog(this, "转账成功，转账金额：" + transferMoney);
                    String sql = "UPDATE t_user SET u_balance =" + frame.getLoginUser().getBalance() +
                            " WHERE u_account = '" + frame.getLoginUser().getAccount() + "'";
                    System.out.println(sql);
                    Util.insert(sql);
                    String sql1 = "UPDATE t_user SET u_balance =" + frame.getUsersList().get(transferUserIndex).getBalance() +
                            " WHERE u_account = '" + frame.getUsersList().get(transferUserIndex).getAccount() + "'";
                    System.out.println(sql1);
                    Util.insert(sql1);
                    iptTransferUser.setText("");
                    iptTransferMoney.setText("");
                } else {
                    JOptionPane.showMessageDialog(this, "余额不足，请重新输入");
                }
            } else {
                JOptionPane.showMessageDialog(this, "没有找到该用户，请重新输入");
            }
        }
    }
}


